package com.mengniu.commerce.collect.service.impl;


import com.mengniu.commerce.collect.common.enums.AcctStatusEnum;
import com.mengniu.commerce.collect.common.enums.CustomerStatusEnum;
import com.mengniu.commerce.collect.common.enums.WmResponseEnum;
import com.mengniu.commerce.collect.service.WmMemberService;
import com.mengniu.commerce.collect.service.WmPointService;
import com.mengniu.commerce.collect.third.request.IWeiMengPointRequest;
import com.mengniu.commerce.collect.third.response.member.MemberListResponse;
import com.mengniu.commerce.collect.third.response.point.PointResponse;
import com.mengniu.commerce.collect.util.RestTemplateUtil;
import com.mengniu.commerce.common.core.domain.R;
import com.mengniu.commerce.sync.api.RemotePointService;
import com.mengniu.commerce.sync.api.domain.PointsIncreaseModel;
import com.mengniu.commerce.sync.api.domain.PointsReduceModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * 用户积分
 *
 * @author yz_sm
 */
@Service
@Slf4j
public class WmPointServiceImpl implements WmPointService {

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Autowired
    private IWeiMengPointRequest weiMengPointRequest;

    @Resource
    private WmMemberService wmMemberService;

    @Resource
    private RemotePointService remotePointService;


    /***
    * @Description: 用户积分查询
    * @Param: []
    * @return: void
    * @Author: zfw
    * @Date: 2023/11/30
    * @Date: 10:32
    */
    @Override
    public String getMemberPoint(String wid) {
        String token = restTemplateUtil.getToken(4001169348805L);
        String totalPoint ="0";

        PointResponse point = weiMengPointRequest.getMemberPoint(wid,token);
        if (ObjectUtils.isEmpty(point.getData()) && WmResponseEnum.SUCCESS.getStatus().equals(point.getCode().getErrcode())){
            //账号正常的数据再计算积分
            if (AcctStatusEnum.STU_A.getStatus().equals(point.getData().getAcctStatus())
                && CustomerStatusEnum.STU_1.getStatus() == point.getData().getCustomerStatus()
            ){
                totalPoint = point.getData().getTotalPoint();
            }
        }
        return totalPoint;
    }

    /**
     * 初始化积分
     */
    @Override
    public void InitializePoint(){
        //查询用户列表
        List<MemberListResponse> list = wmMemberService.getWeiMengUserList();
        for (MemberListResponse s:list){
            increasePoint(s);
        }
    }

    /***
    * @Description: 新增积分
    * @Param:
    * @return:
    * @Author: zfw
    * @Date: 2023/11/30
    * @Date: 21:08
    */
    public R<?> increasePoint(MemberListResponse memberListResponse){
        //获取用户积分
        String memberPoint = getMemberPoint(memberListResponse.getWid());
        if (Integer.valueOf(memberPoint)>0){
            //拼装积分增加入参
            PointsIncreaseModel pointsIncreaseModel = new PointsIncreaseModel();
            pointsIncreaseModel.setMobile(memberListResponse.getPhone());
            pointsIncreaseModel.setChangeReasonCode("初始化积分");
            pointsIncreaseModel.setNote("初始化积分");
            pointsIncreaseModel.setPointsValue(Integer.valueOf(memberPoint));
            //todo 和蒙牛确认
            pointsIncreaseModel.setPointsTemplateId(1);
            //调用积分增加接口
            remotePointService.increasePoint(pointsIncreaseModel,"weimeng-point-collect");
        }else{
            log.info("{}用户,积分为0,无需处理,微盟id为{}",memberListResponse.getPhone(),memberListResponse.getWid());
        }
       return R.ok("新增积分成功");
    }

    /***
     * @Description: 扣减积分
     * @Param:
     * @return:
     * @Author: zfw
     * @Date: 2023/11/30
     * @Date: 21:08
     */
    public R<?> decreasePoint(String phone,int value){
        PointsReduceModel pointsReduceModel = new PointsReduceModel();
        pointsReduceModel.setMobile(phone);
        pointsReduceModel.setChangeReasonCode("兑换商品");
        pointsReduceModel.setPointsValue(value);
        //todo 和蒙牛确认
        pointsReduceModel.setPointsTemplateId(1);
        //调用积分减少接口
        remotePointService.decreasePoint(pointsReduceModel,"weimeng-point-collect");
        return R.ok("扣减积分成功");
    }

}
